package Control;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;

import javax.swing.JOptionPane;
import javax.swing.SwingWorker;

import Entity.Workshop_BookWorkshopEntity;
import Entity.Workshop_BookingStatusCode;
import Entity.Workshop_WorkshopEntity;
import Form.Workshop_ViewWorkshop_Form;

public class Workshop_ViewWorkshop_Controller {
	//Object Attributes
	boolean residentBooked;
	private Workshop_WorkshopEntity viewWorkshop;
	private Workshop_BookWorkshopEntity checkWorkshop = new Workshop_BookWorkshopEntity();
	private Workshop_BookWorkshopEntity bookWorkshop = new Workshop_BookWorkshopEntity();
	private Workshop_BookingStatusCode statusCode = new Workshop_BookingStatusCode("BOK");	
	
	private Workshop_ViewWorkshop_Form parser;

	public Workshop_ViewWorkshop_Form getParser() {
		return parser;
	}

	public void setParser(Workshop_ViewWorkshop_Form parser) {
		this.parser = parser;
	}
	
	public Workshop_ViewWorkshop_Controller(final Workshop_ViewWorkshop_Form parser){
		this.setParser(parser);
		if(parser.getParser() != null){
			viewWorkshop = parser.getParser().getControl().getSelectedWorkshop();
		}
		else if(parser.getViewParser() != null){
			viewWorkshop = parser.getViewParser().getControl().getSelectedWorkshop();
		}
		
		SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>()
		{
			@Override
			protected Void doInBackground() throws Exception {
				viewWorkshop.retrieveAWorkshop();

				checkWorkshop.setWorkshop(viewWorkshop);
				checkWorkshop.setNeedyResident(CommonControls.getUser());
				residentBooked = checkWorkshop.retrieveABooking();
				
				return null;
			}
			
			protected void done(){
				SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
				
				getParser().getLblWorkshopid().setText(viewWorkshop.getWorkshopId());
				getParser().getWorkshopdate().setText(sdf.format(viewWorkshop.getWorkshopDateTime()));
				getParser().getLblTitle().setText(viewWorkshop.getWorkshopName());
				getParser().getCategory().setText(viewWorkshop.getCategory().getDescription());
				getParser().getDescription().setText(viewWorkshop.getDescription());
				getParser().getVenue().setText(viewWorkshop.getVenue());
				getParser().getStartdatetime().setText(sdf.format(viewWorkshop.getStartDateTime()));
				getParser().getEnddatetime().setText(sdf.format(viewWorkshop.getEndDateTime()));
				getParser().getCapacity().setText(Integer.toString(viewWorkshop.getCapacity()));
				getParser().getTrainer().setText(viewWorkshop.getTrainer().getTrainerName());
				getParser().getStatus().setText(viewWorkshop.getStatusCode().getDescription());
				
				if(checkWorkshop.getWorkshop().getStatusCode().getStatusCode().equals("AVA") || checkWorkshop.getWorkshop().getStatusCode().getStatusCode().equals("FUL")){
					if(!residentBooked && !checkWorkshop.getWorkshop().getStatusCode().getStatusCode().equals("FUL")){
						getParser().getBtn().setText("Apply");
	
						getParser().getBtn().addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent arg0) {
								bookWorkshop();
								viewWorkshop.setCapacity(viewWorkshop.getCapacity()-1);
								viewWorkshop.updateCapacity();
								JOptionPane.showMessageDialog(null, "Successfully applied");
								getParser().getParser().getControl().getViewSingle().dispose();
								getParser().getParser().getControl().generateList();
							}
						});
					}
					else{
						getParser().getBtn().setText("Cancel Booking");
						
						getParser().getBtn().addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent arg0) {
								int selection = JOptionPane.showConfirmDialog(null, "Are you sure you want to cancel?", "Confirmation", JOptionPane.YES_NO_OPTION); 
								if(selection == JOptionPane.YES_OPTION){
									cancelBooking();
									viewWorkshop.setCapacity(viewWorkshop.getCapacity()+1);
									viewWorkshop.updateCapacity();
									JOptionPane.showMessageDialog(null, "Booking has been cancelled");
									getParser().getParser().getControl().getViewSingle().dispose();
									getParser().getParser().getControl().generateList();
								}
							}
						});
					}
				}
				else{
					getParser().getBtn().setVisible(false);
				}
				
				CommonControls.togglePanelModality(parser);
				if(parser.getParser() != null){
					parser.getParser().getParser().closeProgressBar();
				}
				else if(parser.getViewParser() != null){
					parser.getViewParser().getParser().closeProgressBar();
				}
			}
		};
		CommonControls.togglePanelModality(parser);
		if(parser.getParser() != null){
			parser.getParser().getParser().getProgressBar();
		}
		else if(parser.getViewParser() != null){
			parser.getViewParser().getParser().getProgressBar();
		}
		worker.execute();
	}
	
	public void bookWorkshop(){
		bookWorkshop.setWorkshop(viewWorkshop);
		bookWorkshop.setNeedyResident(CommonControls.getUser());
		bookWorkshop.setBookingDateTime(CommonControls.getCurrentDate());
		bookWorkshop.setBookingStatusCode(statusCode);
		bookWorkshop.createBooking();
	}
	
	public void cancelBooking(){
		checkWorkshop.setNeedyResident(CommonControls.getUser());
		checkWorkshop.deleteBooking();
	}
}